package com.itheima.mall.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mall.dto.CouponDto;
import com.itheima.mall.mapper.CouponMapper;
import com.itheima.mall.pojo.Coupon;
import com.itheima.mall.pojo.CouponProductCategoryRelation;
import com.itheima.mall.pojo.CouponProductRelation;
import com.itheima.mall.service.SmsCouponService;
import com.itheima.mall.utils.Constants;
import com.itheima.mall.vo.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SmsCouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements SmsCouponService {

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 添加优惠券
     */
    @Override
    @Transactional
    public void create(CouponDto couponParam) {
        //将基本数据存入sms_coupon中
        couponMapper.insert(couponParam);
        //优惠券和产品分类关系存入表中
        List<CouponProductCategoryRelation> pcrList = couponParam.getProductCategoryRelationList();
        if (CollUtil.isNotEmpty(pcrList)) {
            pcrList = pcrList.stream().map(item -> {
                item.setCouponId(couponParam.getId());
                return item;
            }).collect(Collectors.toList());
            //要将couponId塞进去
            couponMapper.saveCPCRList(pcrList);
        }
        //优惠券和产品关系存入表中
        List<CouponProductRelation> prList = couponParam.getProductRelationList();
        if (CollUtil.isNotEmpty(prList)) {
            //要将couponId塞进去
            prList = prList.stream().map(item -> {
                item.setCouponId(couponParam.getId());
                return item;
            }).collect(Collectors.toList());
            couponMapper.saveCPRList(prList);
        }


    }

    /**
     * 删除优惠券
     */
    @Override
    @Transactional
    public void delete(Long id) {
        //删除优惠券基本信息
        couponMapper.deleteById(id);
        //删除优惠券和产品分类关系
        couponMapper.deleteCpcById(id);
        //删除优惠券和产品关系
        couponMapper.deleteCpById(id);

    }

    /**
     * 根据优惠券名称和类型分页获取优惠券列表
     */
    @Override
    public PageResult list(String name, Integer pageNum, Integer pageSize, Integer type) {
        Page<Coupon> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Coupon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(name != null, Coupon::getName, name);
        queryWrapper.eq(type != null, Coupon::getType, type);
        Page<Coupon> pageInfo = couponMapper.selectPage(page, queryWrapper);
        return new PageResult(pageNum, pageSize, (int) pageInfo.getTotal(), pageInfo.getRecords());
    }

    /**
     * 修改优惠券
     */
    @Override
    @Transactional
    public void update(CouponDto couponParam, Long id) {
        //修改基础表
        couponParam.setId(id);
        couponMapper.updateById(couponParam);
        //修改优惠券和产品分类关系
        List<CouponProductCategoryRelation> pcrList = couponParam.getProductCategoryRelationList();
        if (CollUtil.isNotEmpty(pcrList)) {
            couponMapper.updateCPCRList(pcrList);
        }
        //修改优惠券和产品关系
        List<CouponProductRelation> prList = couponParam.getProductRelationList();
        if (CollUtil.isNotEmpty(prList)) {
            couponMapper.updateCPRList(prList);
        }
    }

    /**
     * 获取单个优惠券的详细信息
     */
    @Override
    @Transactional
    public CouponDto coupon(Long id) {
        //查询基本数据
        Coupon coupon = couponMapper.selectById(id);
        //查询优惠券和产品分类关系
        List<CouponProductCategoryRelation> list1 = couponMapper.selectCpcById(id);
        //查询优惠券和产品关系
        List<CouponProductRelation> list2 = couponMapper.selectCpById(id);
        CouponDto couponDto = new CouponDto();
        BeanUtils.copyProperties(coupon, couponDto);
        couponDto.setProductCategoryRelationList(list1);
        couponDto.setProductRelationList(list2);
        return couponDto;
    }


}
